package com.left.tcc.order.service;

import com.left.tcc.coupon.api.CouponApi;
import com.left.tcc.coupon.dto.CouponDTO;
import com.left.tcc.inventory.api.InventoryApi;
import com.left.tcc.inventory.dto.InventoryDTO;
import com.left.tcc.order.dto.OrderDTO;
import com.left.tcc.order.entity.Order;
import com.left.tcc.order.enums.OrderStatusEnum;
import com.left.tcc.order.mapper.OrderMapper;
import com.left.tcc.score.api.ScoreAccountApi;
import com.left.tcc.score.dto.ScoreAccountDTO;
import lombok.extern.slf4j.Slf4j;
import org.dromara.hmily.annotation.Hmily;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Slf4j
@Service
public class OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private InventoryApi inventoryApi;
    @Autowired
    private ScoreAccountApi scoreAccountApi;
    @Autowired
    private CouponApi couponApi;

    @Hmily(confirmMethod = "confirmMethod", cancelMethod = "cancelMethod")
    @Transactional
    public Order createOrder(OrderDTO order) {
        Order ord = new Order();
        BeanUtils.copyProperties(order, ord);
        ord.setStatus(OrderStatusEnum.CONFIRM.getCode());
        this.orderMapper.insertSelective(ord);
        //调用库存服务
        InventoryDTO inventoryDTO = new InventoryDTO();
        inventoryDTO.setCount(ord.getProductCount());
        inventoryDTO.setOrderNo(ord.getOrderNo());
        inventoryDTO.setProductId(ord.getProductId());
        inventoryDTO.setUserId(ord.getUserId());
        this.inventoryApi.decrease(inventoryDTO);

        //调用积分服务
        ScoreAccountDTO scoreAccountDTO = new ScoreAccountDTO();
        scoreAccountDTO.setOrderNo(ord.getOrderNo());
        scoreAccountDTO.setScore(ord.getDecreaseScore());
        scoreAccountDTO.setUserId(ord.getUserId());
        this.scoreAccountApi.decrease(scoreAccountDTO);
        //调用优惠券服务

        //扣优惠卷
        CouponDTO couponDTO = new CouponDTO();
        couponDTO.setUserId(ord.getUserId());
        couponDTO.setOrderNo(ord.getOrderNo());
        couponDTO.setCouponId(ord.getCouponReceiveId());
        this.couponApi.decrease(couponDTO);
        return ord;
    }

    public void confirmMethod(OrderDTO order) {
        log.debug("==========> 订单confirm方法");
        Order ord = new Order();
        ord.setOrderNo(order.getOrderNo());
        ord = this.orderMapper.selectOne(ord);
        ord.setStatus(OrderStatusEnum.NOT_PAY.getCode());
        this.orderMapper.updateByPrimaryKey(ord);
    }

    public void cancelMethod(OrderDTO order) {
        log.debug("==========> 订单cancel方法");
        Order ord = new Order();
        ord.setOrderNo(order.getOrderNo());
        ord = this.orderMapper.selectOne(ord);
        if (ord == null) {
            return;
        }
        ord.setStatus(OrderStatusEnum.CONFIRM_FAIL.getCode());
        this.orderMapper.updateByPrimaryKey(ord);
    }


    public List<Order> unpayOrderList() {

        Example example = Example.builder(Order.class).build();
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("status", OrderStatusEnum.NOT_PAY.getCode());

        List<Order> orderList = this.orderMapper.selectByExample(example);
        return orderList;
    }

    public List<Order> findAll() {
        List<Order> orderList = this.orderMapper.selectAll();
        return orderList;
    }

    public Order findOrderNo(String orderno) {
        Order order = new Order();
        order.setOrderNo(orderno);
        order = this.orderMapper.selectOne(order);
        return order;
    }
}
